package com.huidu.bitrade.controller.common;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Date;

@Slf4j
public class UploadControllerTestBySTS {

//    private OSSClient client = null;
    private static String regionId = "cn-shanghai";
    private static String endpoint;
//    private static String accessKeyId;
//    private static String accessKeySecret;
    private static String stsEndpoint;
    private static String stsAccessKeyId;
    private static String stsAccessKeySecret;
    private static String bucketName;
    private static String roleArn;

    @Data
    public static class AccessInfoByTest{
        private static String regionId = "cn-shanghai";
        private static String endpoint = "oss-cn-shanghai.aliyuncs.com";
//    private String accessKeyId = "LTAI5tLQdBfWbhe5JDzGXTgW";
//    private String accessKeySecret = "QawQak8PSIPLOmFmnkmpVMmZQ6gZS7";
        private static String stsEndpoint = "sts.cn-shanghai.aliyuncs.com";
        private static String stsAccessKeyId = "LTAI5tHC8jTV71XeCF3AaAXy";
        private static String stsAccessKeySecret = "yNf9NA94mgGwTCxaO4Q84iUQKc50g6";
        private static String bucketName = "test-primary";
        private static String roleArn = "acs:ram::1142117875824335:role/oss-api-rule";
    }

    @Data
    public static class AccessInfoByProd{
        private static String regionId = "cn-shanghai";
        private static String endpoint = "oss-cn-shanghai.aliyuncs.com";
        //    private String accessKeyId = "LTAI5tLQdBfWbhe5JDzGXTgW";
//    private String accessKeySecret = "QawQak8PSIPLOmFmnkmpVMmZQ6gZS7";
        private static String stsEndpoint = "sts.cn-shanghai.aliyuncs.com";
        private static String stsAccessKeyId = "LTAI5tHC8jTV71XeCF3AaAXy";
        private static String stsAccessKeySecret = "yNf9NA94mgGwTCxaO4Q84iUQKc50g6";
        private static String bucketName = "test-primary";
        private static String roleArn = "acs:ram::1142117875824335:role/oss-api-rule";
    }

    static {
//        regionId = AccessInfoByTest.regionId;
//        endpoint = AccessInfoByTest.endpoint;
//        stsEndpoint = AccessInfoByTest.stsEndpoint;
//        stsAccessKeyId = AccessInfoByTest.stsAccessKeyId;
//        stsAccessKeySecret = AccessInfoByTest.stsAccessKeySecret;
//        bucketName = AccessInfoByTest.bucketName;
//        roleArn = AccessInfoByTest.roleArn;
    }

    @Test
    public void uploadOssImageSTS() throws ClientException, FileNotFoundException {
        DefaultProfile.addEndpoint(regionId,"Sts",stsEndpoint);
        IClientProfile profile = DefaultProfile.getProfile(regionId, stsAccessKeyId, stsAccessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        String policy = "{\n" +
                "    \"Version\": \"1\", \n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Action\": [\n" +
                "                \"oss:PutObject\",\n" +
                "                \"oss:GetObject\"\n" +
                "            ], \n" +
                "            \"Resource\": [\n" +
                "                \"acs:oss:*:*:test-primary/*\" \n" +
                "            ], \n" +
                "            \"Effect\": \"Allow\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        final AssumeRoleRequest request = new AssumeRoleRequest();
        // 适用于Java SDK 3.12.0及以上版本。
        request.setSysMethod(MethodType.POST);
        request.setRoleArn(roleArn);
        request.setRoleSessionName("oss-api-test");
        request.setPolicy(policy);
        request.setDurationSeconds(3600L);
        final AssumeRoleResponse response = client.getAcsResponse(request);
        log.info("Expiration: " + response.getCredentials().getExpiration());
        log.info("Access Key Id: " + response.getCredentials().getAccessKeyId());
        log.info("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
        log.info("Security Token: " + response.getCredentials().getSecurityToken());
        log.info("RequestId: " + response.getRequestId());

        String ossPath = "2024/04/01/test5.jpg";
        File file = new File("C:\\Users\\a\\Desktop\\微信图片_20230616090558.jpg");

        //使用临时accessKeyId
        OSS ossClient = new OSSClientBuilder().build(endpoint,
                response.getCredentials().getAccessKeyId(),
                response.getCredentials().getAccessKeySecret(),
                response.getCredentials().getSecurityToken());

        //上传文件
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new FileInputStream(file));
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        System.out.println(putObjectResult.getETag());

        // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
        Date expiration = new Date(new Date().getTime() + 200000 * 1000);
        URL url = ossClient.generatePresignedUrl(bucketName, ossPath, expiration);
        System.out.println(url.toString());
    }


//    public OSSClient initClient(String accessKeyId,String accessKeySecret) {
//        if (null == client) {
//            ClientConfiguration conf = new ClientConfiguration();
//            conf.setConnectionTimeout(5000);
//            conf.setMaxErrorRetry(10);
//            CredentialsProvider provider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
//            client = new OSSClient(endpoint, provider, conf);
//        }
//        return client;
//    }
//    public String getUrl(String key,String accessKeyId,String accessKeySecret) {
//        initClient(accessKeyId, accessKeySecret);
//        // 设置URL过期时间为1小时。
//        Date expiration = new Date(new Date().getTime() + 200000 * 1000);
//        // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
//        URL url = client.generatePresignedUrl(bucketName, key, expiration);
//        return url.toString();
//    }
}
